﻿using System;
using IntelligentStorWpfApp.Interface;
using IntelligentStorWpfApp.Model;
using MySql.Data.MySqlClient;
using IntelligentStorWpfApp.Helper;
using System.Data;

namespace IntelligentStorWpfApp.Services
{
    public class LinkProductPositionServices : LinkProductPositionInterface
    {
        string sqlStr = "";
        //link draft
        public bool addLinkDraft(LinkProductPosition linkpp)
        {
            sqlStr = "INSERT INTO c_linkprodposition_draft(ProductId, PositionId, SetTime, Action)" +
                        "VALUES(@ProductId, @PositionId,@SetTime, @Action); ";

            MySqlParameter[] paras =
            {
                new MySqlParameter ("@ProductId",linkpp.ProductId),
                new MySqlParameter ("@PositionId",linkpp.PositionId),
                new MySqlParameter ("@SetTime",linkpp.SetTime),
                new MySqlParameter ("@Action",linkpp.Action)
            };
            return DBHelper.ExecuteNonQuery(sqlStr, paras);
        }

        public bool deleteLinkDraft(int Id)
        {
            sqlStr = "delete from c_linkprodposition_draft where ID=@Id;";
            MySqlParameter[] paras =
            {
                new MySqlParameter("@Id",Id),
            };
            return DBHelper.ExecuteNonQuery(sqlStr, paras);
        }

        public bool deleteLinkDraftByPosition(int pId)
        {
            sqlStr = "delete from c_linkprodposition_draft where PositionId=@pId;";
            MySqlParameter[] paras =
            {
                new MySqlParameter("@pId",pId),
            };
            return DBHelper.ExecuteNonQuery(sqlStr, paras);
        }

        public DataTable getAllLinksDraft()
        {
            sqlStr = "SELECT * FROM c_linkprodposition_draft;";
            return DBHelper.ExecuteDataSet(sqlStr, null);
        }

        public DataTable GetDraftLinkByPositionId(int pId)
        {
            sqlStr = "SELECT * FROM c_linkprodposition_draft where PositionId=@PositionId;";
            MySqlParameter[] paras =
            {
                new MySqlParameter("@PositionId",pId)
            };

            return DBHelper.ExecuteDataSet(sqlStr, paras);
        }
        public DataTable GetDraftLinkByProductId(string productId)
        {
            sqlStr = "SELECT * FROM c_linkprodposition_draft where ProductId=@ProductId;";
            MySqlParameter[] paras =
            {
                new MySqlParameter("@ProductId",productId)
            };

            return DBHelper.ExecuteDataSet(sqlStr, paras);
        }

        public bool updateLinkDraft(LinkProductPosition linkpp)
        {
            throw new NotImplementedException();
        }
        //link
        public bool deleteLinkByPosition(int pId)
        {
            sqlStr = "delete from c_linkprodposition where PositionId=@PositionId; ";
            MySqlParameter[] paras =
            {
                new MySqlParameter("@PositionId",pId),
            };

            return DBHelper.ExecuteNonQuery(sqlStr, paras);
        }

        public bool deleteLinkByProduct(string productId)
        {
            sqlStr = "delete from c_linkprodposition where ProductId=@ProductId; ";
            MySqlParameter[] paras =
            {
                new MySqlParameter("@ProductId",productId),
            };

            return DBHelper.ExecuteNonQuery(sqlStr, paras);
        }

        public LinkProductPosition getLinkByProductId(string productId)
        {
            sqlStr = "SELECT * FROM c_linkprodposition where ProductId=@ProductId;";
            LinkProductPosition linkPosition = new LinkProductPosition();
            MySqlParameter[] paras =
            {
                new MySqlParameter("@ProductId",productId)
            };

            DataTable dt = DBHelper.ExecuteDataSet(sqlStr, paras);
            if(dt.Rows.Count>0)
            {
                linkPosition.ProductId = dt.Rows[0]["ProductId"].ToString();
                linkPosition.PositionId = Convert.ToInt32( dt.Rows[0]["PositionId"]);
            }
            return linkPosition;
        }

        public bool linkProductPosition(string productId, int positionId)
        {
            sqlStr = "INSERT INTO c_linkprodposition ( ProductId, PositionId )" +
            "VALUES (@ProductId,@PositionId); ";
            MySqlParameter[] paras =
            {
                new MySqlParameter("@ProductId",productId),
                new MySqlParameter("@PositionId",positionId)
            };

            return DBHelper.ExecuteNonQuery(sqlStr, paras);
        }

        public DataTable GetLinkByPositionId(int pId)
        {

            sqlStr = "SELECT * FROM c_linkprodposition where PositionId=@PositionId;";
            MySqlParameter[] paras =
            {
                new MySqlParameter("@PositionId",pId)
            };

            DataTable dt = DBHelper.ExecuteDataSet(sqlStr, paras);
            return dt;

        }

       
    }
}
